package com.aivin.mobileApi;

import com.aivin.entity.*;
import com.aivin.service.PicService;
import com.aivin.service.UserService;
import com.aivin.utils.MyStringUtils;
import com.aivin.utils.MyValidateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *  restful设计
 */
@Controller//@Service @Component
@RequestMapping("/mobileApi")
public class UserFollowController {


    @Autowired
    private UserService userService  ;



    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/getfollowdStateByUser/1/3
     *  查询对某人的关注情况
     * **/
    @RequestMapping(value = "/getfollowdStateByUser/{followdUserId}/{userId}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  getfollowdStateByUser(@PathVariable int followdUserId   ,@PathVariable int userId  ){
        int state =userService.getfollowdStateByUser(followdUserId ,userId) ;

        int followState =  0 ; //默认为 0状态
        if(state == -1){
            int recodId =  addFollowdStateByUserId(followdUserId ,userId ,followState);
            state =  recodId >0 ?  followState : -1 ;
        }else{
            //..
        }
        return state+"" ;
    }

    /***
     *  http://192.168.0.119:8081/findYou/mobileApi/getfollowdListByUserId/3/0/20
     *   查询 关注用户列表
     * **/
    @RequestMapping(value = "/getfollowdListByUserId/{userId}/{offset}/{limit}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  getfollowdListByUserId(@PathVariable int userId   , @PathVariable int offset ,@PathVariable int limit  ){
        String resultStr="noData" ;
       List<FollowUserBean>  list  =userService.getfollowdListByUserId(userId ,offset ,limit) ;

        if( MyValidateUtil.isEmpty(list)){
           //
        }else{
            List<FollowUserDetailBean> resultList = new ArrayList<FollowUserDetailBean>() ;
            for(FollowUserBean bean : list){
                FollowUserDetailBean detailBean = new FollowUserDetailBean() ;
                TbUserV1 bv1= getUserBeanInfoByUserIdV1(bean.getFollowId());

                detailBean.setUserId(bv1.getId()); ;

                detailBean.setNickName(bv1.getNickName()) ;
                detailBean.setUserInfo(bv1.getUserInfo()) ;
                detailBean.setUserImageUrl(bv1.getUserImagURL()) ;
                detailBean.setRegTime(bean.getRegTime()) ; //注意：这个时间是开始关注的时间
                resultList.add(detailBean) ;

            }
            resultStr =JSON.toJSONString(resultList) ;
        }

        return  resultStr ;
    }


    /**根据用户Id查询用户信息**/
    public TbUserV1  getUserBeanInfoByUserIdV1( int userId    ) {

        TbUserV1  bean  =   userService.getUserInfoByUserIdV1(userId) ;
        return bean;
    }


    public int   addFollowdStateByUserId(  int followdUserId ,    int userId  ,  int state){
        FollowUserBean  bean  = new FollowUserBean() ;
        bean.setUserFollowedId(followdUserId);
        bean.setUserId(userId);
        bean.setState(state);
        bean.setRegTime(MyStringUtils.getCurrentTimeStr());
        int  recodId =  userService.addFollowdStateByUserId(bean) ;
        return  recodId   ;
    }


    /***
     *
     http://192.168.0.119:8081/findYou/mobileApi/setFollowedStateByUserId/1/1/1
     */
    @RequestMapping(value = "/setFollowedStateByUserId/{userFollowedId}/{userId}/{followedState}",
            method = {RequestMethod.POST, RequestMethod.GET},
            produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  setFollowedStateByUserId(@PathVariable int userFollowedId   ,@PathVariable int userId  ,@PathVariable int followedState ){

        int state =userService.setFollowedStateByUserId(userFollowedId,userId ,followedState) ;
        state = state > 0 ? followedState :  -1 ;
        return state+"" ;

    }




}
